﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Cores.KeyAssignElems;

namespace Krile.Forms.Dialogs.Popup
{
    public partial class InitialAssign : Form
    {
        public InitialAssign()
        {
            InitializeComponent();
        }

        private void styleUpdateChecked(object sender, EventArgs e)
        {
            labelNone.Visible = styleNone.Checked;
            labelDefault.Visible = styleDefault.Checked;
            labelKrile.Visible = styleKrile.Checked;
            labelKeyboard.Visible = styleKeyboard.Checked;
            labelP3.Visible = styleP3.Checked;
            labelTween.Visible = styleTween.Checked;
        }

        private void continueBtn_Click(object sender, EventArgs e)
        {
            var lists = Core.KeyAssign.GetCandidatesArray();
            List<KeyAssignData> newDatas = new List<KeyAssignData>();
            if (styleNone.Checked)
            {
                //noop
            }
            else
            {
                //basic assignment
                newDatas.Add(Make(Keys.Down, "SelectBelow", lists));
                newDatas.Add(Make(Keys.Up, "SelectAbove", lists));
                newDatas.Add(Make(Keys.Down | Keys.Shift, "SelectNewBelow", lists));
                newDatas.Add(Make(Keys.Up | Keys.Shift, "SelectNewAbove", lists));
                newDatas.Add(Make(Keys.PageDown, "ScrollBelow", lists));
                newDatas.Add(Make(Keys.PageUp, "ScrollAbove", lists));
                newDatas.Add(Make(Keys.Home, "ScrollTop", lists));
                newDatas.Add(Make(Keys.End, "ScrollEnd", lists));
                newDatas.Add(Make(Keys.F5, "RefreshTimeline", lists));
                newDatas.Add(Make(Keys.C | Keys.Control, "CopyStatus", lists));
                newDatas.Add(Make(Keys.C | Keys.Control | Keys.Shift, "CopyUserID", lists));
                newDatas.Add(Make(Keys.Tab, "TogglePostbox", true, lists));

                if (styleP3.Checked)
                {
                    //P3 style assignment
                    newDatas.Add(Make(Keys.Enter | Keys.Control, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.Oemcomma, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.OemPeriod, "SelectRightTab", lists));
                    newDatas.Add(Make(Keys.D | Keys.Control, "SetAlreadyReadedAll", lists));
                    newDatas.Add(Make(Keys.E | Keys.Control, "ToggleSearchBar", lists));
                    newDatas.Add(Make(Keys.F | Keys.Control, "DefaultFav", lists));
                    newDatas.Add(Make(Keys.G | Keys.Control, "AddTabFromUser", lists));
                    newDatas.Add(Make(Keys.I | Keys.Control, "OpenPostbox", lists));
                    newDatas.Add(Make(Keys.L | Keys.Control, "ClosePostbox", true, lists));
                    newDatas.Add(Make(Keys.O | Keys.Control, "ScrollTop", lists));
                    newDatas.Add(Make(Keys.P | Keys.Control, "TogglePostbox", true, lists));
                    newDatas.Add(Make(Keys.Q | Keys.Control, "AddQuickTab", lists));
                    newDatas.Add(Make(Keys.R | Keys.Control, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.S | Keys.Control, "ShowConfig", lists));
                    newDatas.Add(Make(Keys.T | Keys.Control, "RefreshTimeline", lists));

                    newDatas.Add(Make(Keys.D0, "ScrollTop", lists));
                    newDatas.Add(Make(Keys.D1, "Select1stTab", lists));
                    newDatas.Add(Make(Keys.D2, "Select2ndTab", lists));
                    newDatas.Add(Make(Keys.D3, "Select3rdTab", lists));
                    newDatas.Add(Make(Keys.D4, "Select4thTab", lists));
                    newDatas.Add(Make(Keys.D5, "Select5thTab", lists));

                    newDatas.Add(Make(Keys.A, "JumpInReplyTo", lists));
                    newDatas.Add(Make(Keys.C, "ShowStatusPage", lists));
                    newDatas.Add(Make(Keys.F, "DefaultFav", lists));
                    newDatas.Add(Make(Keys.G, "AddTabFromUser", lists));
                    newDatas.Add(Make(Keys.H, "ShowUserPage", lists));
                    newDatas.Add(Make(Keys.R, "JumpInReplyTo", lists));
                    newDatas.Add(Make(Keys.W, "ShowUserPage", lists));
                }
                else if (styleTween.Checked)
                {
                    //Tween style assignment
                    newDatas.Add(Make(Keys.Space, "SelectNewBelow", lists));
                    newDatas.Add(Make(Keys.PageUp | Keys.Control, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.PageDown | Keys.Control, "SelectRightTab", lists));
                    newDatas.Add(Make(Keys.Tab | Keys.Control, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.Tab | Keys.Shift | Keys.Control, "SelectRightTab", lists));
                    newDatas.Add(Make(Keys.Enter, "DefaultReply", lists));

                    newDatas.Add(Make(Keys.A, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.H, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.J, "SelectBelow", lists));
                    newDatas.Add(Make(Keys.K, "SelectAbove", lists));
                    newDatas.Add(Make(Keys.L, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.S, "SelectRightTab", lists));
                    newDatas.Add(Make(Keys.Z, "EnsureVisibleTop", lists));

                    newDatas.Add(Make(Keys.B | Keys.Control, "SetAlreadyReadedAll", lists));
                    newDatas.Add(Make(Keys.D | Keys.Control, "SetAlreadyReadedAll", lists));
                    newDatas.Add(Make(Keys.F | Keys.Control, "ToggleSearchBar", lists));
                    newDatas.Add(Make(Keys.G | Keys.Control, "SelectNewestStatus", lists));
                    newDatas.Add(Make(Keys.J | Keys.Shift, "SelectNewBelow", lists));
                    newDatas.Add(Make(Keys.K | Keys.Shift, "SelectNewAbove", lists));
                    newDatas.Add(Make(Keys.O | Keys.Control, "ShowUserPage", lists));
                    newDatas.Add(Make(Keys.P | Keys.Control, "TogglePostbox", true, lists));
                    newDatas.Add(Make(Keys.Q | Keys.Control, "AddQuickTab", lists));
                    newDatas.Add(Make(Keys.R | Keys.Control, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.S | Keys.Control, "DefaultFav", lists));
                }
                else if (styleKrile.Checked)
                {
                    newDatas.Add(Make(Keys.R, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.F, "DefaultFav", lists));
                    newDatas.Add(Make(Keys.V, "SelectBelow", lists));
                    newDatas.Add(Make(Keys.D, "SelectAbove", lists));
                    newDatas.Add(Make(Keys.V | Keys.Shift, "SelectNewBelow", lists));
                    newDatas.Add(Make(Keys.D | Keys.Shift, "SelectNewAbove", lists));
                    newDatas.Add(Make(Keys.D0, "ScrollTop", lists));
                    newDatas.Add(Make(Keys.X, "JumpInReplyTo", lists));
                    newDatas.Add(Make(Keys.W, "ShowStatusPage", lists));
                    newDatas.Add(Make(Keys.Enter, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.Oemcomma, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.OemPeriod, "SelectRightTab", lists));
                    newDatas.Add(Make(Keys.Q, "AddQuickTab", lists));
                    newDatas.Add(Make(Keys.D | Keys.Control, "SetAlreadyReadedAll", lists));
                    newDatas.Add(Make(Keys.P | Keys.Control, "TogglePostbox", true, lists));
                    newDatas.Add(Make(Keys.Q | Keys.Control, "AddQuickTab", lists));
                    newDatas.Add(Make(Keys.R | Keys.Control, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.S | Keys.Control, "ToggleSearchBar", lists));
                }
                else if (styleKeyboard.Checked)
                {
                    newDatas.Add(Make(Keys.R, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.F, "DefaultFav", lists));
                    newDatas.Add(Make(Keys.D0, "ScrollTop", lists));
                    newDatas.Add(Make(Keys.X, "JumpInReplyTo", lists));
                    newDatas.Add(Make(Keys.W, "ShowStatusPage", lists));
                    newDatas.Add(Make(Keys.J, "SelectBelow", lists));
                    newDatas.Add(Make(Keys.K, "SelectAbove", lists));
                    newDatas.Add(Make(Keys.J | Keys.Shift, "SelectNewBelow", lists));
                    newDatas.Add(Make(Keys.K | Keys.Shift, "SelectNewAbove", lists));
                    newDatas.Add(Make(Keys.H, "SelectLeftTab", lists));
                    newDatas.Add(Make(Keys.L, "SelectRightTab", lists));
                    newDatas.Add(Make(Keys.Enter, "OpenPostbox", lists));
                    newDatas.Add(Make(Keys.Escape, "ClosePostbox", true, lists));
                    newDatas.Add(Make(Keys.S | Keys.Control, "ShowConfig", lists));
                    newDatas.Add(Make(Keys.Q, "AddQuickTab", lists));
                    newDatas.Add(Make(Keys.OemQuestion, "ToggleSearchBar", lists));
                    newDatas.Add(Make(Keys.OemQuestion | Keys.Control, "ToggleSearchBar", true, lists));
                    newDatas.Add(Make(Keys.D, "SetAlreadyReadedAll", lists));
                }
                else if (styleDefault.Checked)
                {
                    newDatas.Add(Make(Keys.D | Keys.Control, "SetAlreadyReadedAll", lists));
                    newDatas.Add(Make(Keys.F | Keys.Control, "DefaultFav", lists));
                    newDatas.Add(Make(Keys.P | Keys.Control, "TogglePostbox", true, lists));
                    newDatas.Add(Make(Keys.Q, "AddQuickTab", lists));
                    newDatas.Add(Make(Keys.R | Keys.Control, "DefaultReply", lists));
                    newDatas.Add(Make(Keys.S | Keys.Control, "ToggleSearchBar", lists));
                }
            }
            Core.Config.KeyAssign.KeyAssignData = newDatas.ToArray();
            DialogResult = DialogResult.OK;
        }

        private KeyAssignData Make(Keys link, string id, bool enableInText, KeyAssignCallbackCandidate[] lists)
        {
            return new KeyAssignData()
            {
                LinkedKeys = link,
                InvokeMethodDatas = new[] { new KeyAssignMethodData(GetCandidate(lists, id)) },
                EnableOnTextBox = enableInText
            };
        }

        private KeyAssignData Make(Keys link, string id, KeyAssignCallbackCandidate[] lists)
        {
            return Make(link, id, false, lists);
        }

        private KeyAssignCallbackCandidate GetCandidate(KeyAssignCallbackCandidate[] cands, string id)
        {
            foreach (var c in cands)
            {
                if (c.CallbackMethod.Method.Name == id)
                    return c;
            }
            return null;
        }
    }
}
